package com.example.huichaoliu;

import com.huichaoliu.adapters.CoolPhotoAdapter;
import com.huichaoliu.adapters.ProductAdapter;
import com.huichaoliu.app.R;
import com.rederxu.tools.StringUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class CoolFragment extends Fragment implements OnClickListener,OnItemClickListener,OnScrollListener {

	public final static int TYPE_HELP = 100;
	public final static int TYPE_PHOTO = 101;
	public final static int TYPE_FITTING = 102;
	public final static int CODE_BACK_FROM_FITTING = 103;
	private View v = null;
	private Button btn_comment = null;
	private EditText et_input =null;
	private ListView listView = null;
	private TextView tv_photo = null;
	private TextView tv_help = null;
	private TextView tv_fitting = null;
	private int type = TYPE_PHOTO;
	private View layout_bottom;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if(v == null){
			v = inflater.inflate(R.layout.fragment_cool, null);
			init();
		}
		ViewGroup parent = (ViewGroup) v.getParent();
		if (parent != null) {
			parent.removeView(v);
		}
		return v;
	}
	
	private void init(){
		btn_comment = (Button) v.findViewById(R.id.btn_comment);
		tv_photo = (TextView) v.findViewById(R.id.tv_photo);
		tv_help = (TextView) v.findViewById(R.id.tv_help);
		tv_fitting = (TextView) v.findViewById(R.id.tv_fitting);
		et_input = (EditText) v.findViewById(R.id.et_input);
		layout_bottom = v.findViewById(R.id.layout_bottom);
		listView = (ListView) v.findViewById(R.id.listView);
		listView.setAdapter(new CoolPhotoAdapter(getActivity(),layout_bottom,et_input));
		listView.setOnScrollListener(this);
		listView.setOnItemClickListener(this);
		tv_photo.setOnClickListener(this);
		tv_help.setOnClickListener(this);
		tv_fitting.setOnClickListener(this);
		btn_comment.setOnClickListener(this);
	}

	public int getType(){
		return type;
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.tv_help:
			type = TYPE_HELP;
			tv_help.setBackgroundColor(0xff1E1E1E);
			tv_photo.setBackgroundColor(Color.TRANSPARENT);
			tv_fitting.setBackgroundColor(Color.TRANSPARENT);
			getActivity().findViewById(R.id.right_img_button).setVisibility(View.VISIBLE);
			break;
		case R.id.tv_photo:
			type = TYPE_PHOTO;
			tv_help.setBackgroundColor(Color.TRANSPARENT);
			tv_photo.setBackgroundColor(0xff1E1E1E);
			tv_fitting.setBackgroundColor(Color.TRANSPARENT);
			getActivity().findViewById(R.id.right_img_button).setVisibility(View.VISIBLE);
			break;
		case R.id.tv_fitting:
			type = TYPE_FITTING;
			tv_help.setBackgroundColor(Color.TRANSPARENT);
			tv_photo.setBackgroundColor(Color.TRANSPARENT);
			tv_fitting.setBackgroundColor(0xff1E1E1E);
			getActivity().findViewById(R.id.right_img_button).setVisibility(View.GONE);
			break;
		case R.id.btn_comment:
			String content = et_input.getText().toString();
			if(StringUtils.isBlank(content)){
				Toast.makeText(getActivity(), "请输入评论内容", Toast.LENGTH_SHORT).show();
				return;
			}
			layout_bottom.setVisibility(View.GONE);
			InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);  
			inputMethodManager.hideSoftInputFromWindow(et_input.getWindowToken(),0);
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(type == TYPE_HELP){
			startActivityForResult(new Intent(getActivity(),DetailHelpActivity.class),CODE_BACK_FROM_FITTING);
		}else if(type == TYPE_PHOTO){
			Intent i = new Intent(getActivity(),DetailPhotoActivity.class);
			i.putExtra("type", type);
			startActivity(i);
		}else if(type == TYPE_FITTING){
			startActivityForResult(new Intent(getActivity(),CollocationSchemeActivity.class),CODE_BACK_FROM_FITTING);
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		layout_bottom.setVisibility(View.GONE);
		InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);  
		inputMethodManager.hideSoftInputFromWindow(et_input.getWindowToken(),0);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == CODE_BACK_FROM_FITTING && resultCode == Activity.RESULT_OK){
			((HomeActivity)getActivity()).setViewPagerPosition(1);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
